package gestionCandidats;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.SortedSet;

import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.border.BevelBorder;

import modele.Dossier;
import modele.Etudiant;
import modele.Fiche;
import modele.Parcours;
import modele.UniteEnseignement;

public class FicheAfficheAction
extends AbstractAction  {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	static String[] enteteFiche = {"", "Parcours", "UE Choix 1", "UE Choix 2"};

	GestionCandidats appli;
	DossierAfficheAction doaa;
	DemandeAfficheAction deaa;
	FicheSuiteAction fsa;

	JFrame vue;
	JScrollPane scrollpane;
	JPanel panel, pied, entete;
	JPanel[] jp;
	JComboBox<String>[][] jcb;
	JButton suite, abondon;
	Dossier dos;
	Fiche fic;
	JCheckBox absente;

	protected FicheAfficheAction(GestionCandidats appli,
			DossierAfficheAction doaa,
			DemandeAfficheAction deaa,
			FicheSuiteAction fsa)
	throws Exception {
		super("FicheAfficheAction");
		this.appli = appli;
		this.doaa = doaa;
		this.deaa = deaa;
		this.fsa = fsa;
		vue = appli.getFicheAffiche();
	}

	public Component getVue(){
		return vue;
	}

	public Fiche getFiche(){
		return fic;
	}

	public boolean getAbsente() throws Exception {
		return absente.isSelected();
	}

	public String[][] getUesIdent() throws Exception {
		if (absente.isSelected()) {
			String[][] aux = new String[jcb.length][];
			for (int i=0; i<aux.length; i++) {
				aux[i] = new String[0];
			}
			return aux;
		}
		else {
			SortedSet<UniteEnseignement> ues = appli.getBd().getUniteEnseignements();
			String[][] correspondance = new String[2][ues.size()];
			int ind=0;
			for (UniteEnseignement ue : ues) {
				correspondance[0][ind] = new String(ue.getIdent().toString());
				correspondance[1][ind] = new String(ue.getIntitule());
				ind++;
			}
			String[][] aux = new String[jcb.length][];
			for (int i=0; i<aux.length; i++) {
				aux[i] = new String[jcb[i].length];
				for (int j=0; j<aux[i].length; j++) {
					for (int k=0; k<correspondance[1].length; k++)
						if (correspondance[1][k].
								equals((String)(jcb[i][j].getSelectedItem())))
							aux[i][j] = correspondance[0][k];
				}
			}
			return aux;
		}
	}

	private ActionListener abondonAction() {
		ActionListener abondonListener = new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					dos.abondon();
					vue.setVisible(false);
					doaa.actionPerformed(e);
				}
				catch (Exception ex){}
			}
		};
		return abondonListener;
	}

	private JComboBox<String> getUesJCB(Parcours par, int col, Integer ident) throws Exception {
		// col : 0 pour obligatoire, 1 pour choix 1, 2 pour choix 2
		JComboBox<String> uesJCB = new JComboBox<String>();
		SortedSet<UniteEnseignement> ues;
		switch (col) {
		case 0 : ues = par.getUeObligatoires(); break;
		case 1 : ues = par.getUeChoix1(); break;
		case 2 : ues = par.getUeChoix2(); break;
		default : ues = par.getUeChoix2();
		}
		int ind = 0;
		for (UniteEnseignement ue : ues) {
			uesJCB.addItem(ue.getIntitule());
			if (ue.getIdent().equals(ident))
				uesJCB.setSelectedIndex(ind);
			ind++;
		}
		return uesJCB;
	}

	@SuppressWarnings("unchecked")
	public void actionPerformed(ActionEvent e) {
		try {
			if(vue == null) {
				// l'info
				panel = new JPanel();
				panel.setLayout(new GridLayout(0,2));
				vue = new JFrame();
				vue.getContentPane().add(panel, BorderLayout.CENTER);
				scrollpane = new JScrollPane();
				scrollpane.setBorder(new BevelBorder(BevelBorder.LOWERED));
				panel.add(scrollpane,BorderLayout.CENTER);
				// les boutons
				entete = new JPanel();
				entete.setLayout(new GridLayout(0,2));
				entete.setBackground(GestionCandidats.colorInfo);
				vue.getContentPane().add(entete, BorderLayout.NORTH);
				entete.add(new JLabel(enteteFiche[2]));
				entete.add(new JLabel(enteteFiche[3]));
				pied = new JPanel();
				vue.getContentPane().add(pied, BorderLayout.SOUTH);
				abondon = new JButton("Abondon");
				abondon.addActionListener(abondonAction());
				pied.add(abondon);
				suite = new JButton("Suite");
				suite.addActionListener(fsa); // passer en parametre appel
				pied.add(suite);
				absente = new JCheckBox("Fiche absente");
				pied.add(absente);
			}
			dos = deaa.getDossier();
			fic = fsa.getFiche();
			Etudiant etu = dos.getEtudiant();
			absente.setSelected(fic.getAbsente());
			Parcours parTraite = fic.getParcours();
			vue.setTitle("Dossier "+ etu.getIdApoflux()+
					"          Parcours "+parTraite.getIntitule()+
					"          de "+ etu.getNom()+
					" "+ etu.getPrenom());
			jp = new JPanel[2];
			for (int i=0; i<jp.length; i++){
				jp[i] = new JPanel();
				jp[i].setLayout(new GridLayout(0,1));
			}
			jcb = new JComboBox[2][];
			panel.removeAll();
			panel.add(jp[0]);
			Integer[][] old = fic.getUesDemandeesIdent();
			jcb[0] = new JComboBox[parTraite.getUeChoix1().size()];
			for (int j=0; j<jcb[0].length; j++) {
				jcb[0][j] = getUesJCB(parTraite,1,old[0][j]);
				jp[0].add(jcb[0][j]);
			}
			panel.add(jp[1]);
			jcb[1] = new JComboBox[parTraite.getUeChoix2().size()];
			for (int j=0; j<jcb[1].length; j++) {
				jcb[1][j] = getUesJCB(parTraite,2,old[1][j]);
				jp[1].add(jcb[1][j]);
			}
			scrollpane.setPreferredSize(new Dimension(appli.getWidth()-100,appli.getHeight()-200));
			vue.pack();
			Point p = appli.getLocationOnScreen();
			vue.setLocation(p.x + 50, p.y +100);
			vue.setVisible(true);
		}
		catch(Exception ex){
			JOptionPane.showMessageDialog(appli,ex.getMessage(),"Mise a jour des fiches : Attention",JOptionPane.ERROR_MESSAGE);
			ex.printStackTrace();
		}
	}
}
